package com.example.shopping_web_back.service;

import com.example.shopping_web_back.mapper.GoodsMapper;
import com.example.shopping_web_back.pojo.Goods;
import com.example.shopping_web_back.pojo.PageBean;
import com.example.shopping_web_back.service.impl.GoodsServiceImpl;
import com.example.shopping_web_back.utils.ThreadLocalUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class GoodsService implements GoodsServiceImpl {

    @Autowired
    private GoodsMapper goodsMapper;

    @Override
    public PageBean<Goods> getGoodsData(Integer pageNum, Integer pageSize) {
        //1.创建PageBean
        PageBean<Goods> pbGoods = new PageBean<>();

        //2.开启分页查询
        PageHelper.startPage(pageNum, pageSize);

        //3.调用mapper
        List<Goods> goodsList = goodsMapper.getAllGoodsData();
        PageInfo<Goods> goods = new PageInfo<Goods>(goodsList);
        pbGoods.setTotal(goods.getTotal());
        pbGoods.setItems(goods.getList());
        return pbGoods;
    }

    @Override
    public Goods getGoodsDataByID(String id) {
        return goodsMapper.getGoodsData(id);
    }

    @Override
    public Goods getGoodsDataByBusiness(String business, String title) {
        return goodsMapper.getGoodsDataByBusiness(business, title);
    }

    @Override
    public void addNewGoods(Goods goods) {
        goodsMapper.insertGoods(goods);
    }

    @Override
    public void deleteGoods(String title, String business) {
        goodsMapper.deleteGoods(title,business);
    }

    @Override
    public void updateGoods(String old_title, Goods new_goods) {
        goodsMapper.updateGoods(old_title, new_goods);
    }

    @Override
    public PageBean<Goods> list(Integer pageNum, Integer pageSize, String category) {
        //1.创建PageBean
        PageBean<Goods> pbGoods = new PageBean<>();
        Map<String, Object> map = ThreadLocalUtil.get();
        String businessName = (String) map.get("Business_name");

        //2.开启分页查询
        PageHelper.startPage(pageNum, pageSize);

        //3.调用mapper
        List<Goods> goodsList = goodsMapper.list(businessName, category);
        PageInfo<Goods> goods = new PageInfo<Goods>(goodsList);
        pbGoods.setTotal(goods.getTotal());
        pbGoods.setItems(goods.getList());
        return pbGoods;
    }
}
